package com.Poly01;

//多态的应用：
//动态数组：
//题目:现有一个继承结构，要求创建1个Person对象，2个Student对象和2个Teacher对象，统一放在数组中，并调用每个对象say方法。
//现在增加一些业务：调用子类特有的方法，比如Teacher有一个teach，Student有一个study怎么调用？
public class Test {
    public static void main(String[] args) {
        //父类的引用可以指向子类的对象
        Person[] people = new Person[5];
        people[0] = new Person("jack", 18);
        //向上转型
        people[1] = new Student("tom", 17, 80.0);
        people[2] = new Student("marry", 18, 90.5);
        people[3] = new Teacher("smith", 35, 20000);
        people[4] = new Teacher("jeff", 45, 18000);
        for(int i = 0; i<people.length; i++){
            System.out.println(people[i].say());//此处用到了动态绑定机制
            //注意:people[i]的编译类型是Person，运行类型根据JVM的运行机制决定
            //根据增加业务的需求，可以使用向下转型
            if (people[i] instanceof Student) {//用于判断运行类型
//                Student student = (Student) people[i];
//                student.Study(); 向下转型的体现
                System.out.println(((Student) people[i]).Study());//可以缩写
            } else if (people[i] instanceof Teacher) {
                System.out.println(((Teacher)people[i]).Teach());
            } else if (people[i] instanceof Person) {
            }
            else{
                System.out.println("找不到对象");
            }
        }
    }
}
